import os
import random
from threading import Thread

from app.services.business.gaolu_diagnose.ind import ind_cls_dict
from app.services.business.gaolu_diagnose.ind.abstract_ind import RealTimeInfluxDbInd, AbstractInd
from app.services.chart.Line import Line
from app.utils import date_util, simple_util, string_util
from datetime import timedelta
from app.exts import influxdb
from app.exts import mysql_pool
from datetime import datetime
from app.configs import config_base_dir

import json

from app.utils.thread import CountDownLatch
import logging


class ZhaPiComponent(Line):
    """
    炉渣数据表
    """

    def generate2(self, start, end):

        start, end = date_util.parse_start_end_time(start, end, hour_interval=24)

        table = []
        last = None
        end_temp = start + timedelta(hours=1)
        while start < end and start<=datetime.now():
            random.seed(start.timestamp())
            try:
                input = self.get_input(start, end_temp)
                nianjie_result = round(self.cal_nianjie(input), 2)
                tongleng_result = round(self.cal_tongleng(input), 2)
                zhapi_result = round(random.randint(2000,3000)/100, 2)
                #zhapi_result = round(self.cal_zha_pi(input), 2)
                zhapi_result = round(random.randint(2000,3000)/100, 2)
                ktp = input.get("CG_LT_GL_GL04_KTP")
                rrd_location_result = self.cal_rrd_location(tongleng_result)
                table.append(dict(date=start, nianjie=nianjie_result, tongleng=tongleng_result,
                                  zhapi=zhapi_result, rrd=rrd_location_result, ktp=ktp, input=input))
            except Exception as e:
                logging.error(e)
            start = end_temp
            end_temp = start + timedelta(hours=1)

        return dict(columns=[
            {"title": '时间', "dataIndex": 'date', "key": 'date'},
            {"title": '渣皮厚度', "dataIndex": 'zhapi', "key": 'zhapi'},
            {"title": '粘结指数', "dataIndex": 'nianjie', "key": 'nianjie'},
            {"title": '铜冷占比', "dataIndex": 'tongleng', "key": 'tongleng'},
            {"title": 'KTP', "dataIndex": 'ktp', "key": 'ktp'},
            {"title": '软熔带根部位置', "dataIndex": 'rrd', "key": 'rrd'},
        ], data=table,
            input=last)


    def generate(self,start,end):
        start, end = date_util.parse_start_end_time(start, end, hour_interval=24)
        import pandas as pd
        df = pd.read_csv(os.path.join(config_base_dir, "demo.csv"))

        # df['时间'] = pd.to_datetime(df["时间"], format='%Y-%m-%d %H:%M:%S')
        # df['时间'] = pd.to_datetime(df["时间"]).to_pydatetime()
        # df['时间'] = df["时间"].dt.to_pydatetime()
        # df.to_csv("./demo.csv")
        results = df.to_dict(orient="records")


        table = []
        last = None
        end_temp = start + timedelta(hours=1)
        index = 0
        while start < end and start <= datetime.now():
            i = index % len(results)
            results[i]['date'] = start
            table.append(results[i])

            start = end_temp
            end_temp = start + timedelta(hours=1)
            index = index + 1

        return dict(columns=[
            {"title": '时间', "dataIndex": 'date', "key": 'date'},
            {"title": '渣皮厚度', "dataIndex": 'zhapi', "key": 'zhapi'},
            {"title": '粘结指数', "dataIndex": 'nianjie', "key": 'nianjie'},
            {"title": '铜冷占比', "dataIndex": 'tongleng', "key": 'tongleng'},
            {"title": 'KTP', "dataIndex": 'ktp', "key": 'ktp'},
            {"title": '软熔带根部位置', "dataIndex": 'rrd', "key": 'rrd'},
        ], data=table,
            input=last)



    def get_input(self, start, end):

        """
        渣皮厚度
        :return:
        """

        class SubThread(Thread):
            def __init__(self, name, latch: CountDownLatch, params):
                Thread.__init__(self)
                self.name = name
                self.latch = latch
                self.params = params
                self.result = 0

            def run(self):
                name = self.name
                try:
                    if "db" not in self.params.keys():
                        influxdb = self.params.get("influxdb")
                        influxdb_client = influxdb.get_client()
                        params = self.params

                        instance: RealTimeInfluxDbInd = RealTimeInfluxDbInd(influxdb_client, params)
                        value = instance.get_result()
                        self.result = value
                    else:
                        cls = ind_cls_dict[name]
                        instance: AbstractInd = cls(None, self.params, mysql_pool)
                        value = instance.get_result()
                        self.result = value
                except Exception as e:
                    logging.error("fail to get result of {}. {}".format(name, e), exc_info=1)
                logging.info("{} finished".format(name))
                self.latch.countDown()

        inds = ['CG_LT_GL_GL04_LQBGSZGLLFET1525', 'CG_LT_GL_GL04_RFH', 'CG_LT_GL_GL04_LQBGSZGLLFET1516',
                'CG_LT_GL_GL04_LQBCSWDTE1504', 'CG_LT_GL_GL04_LQBGSZGLLFET1515', 'CG_LT_GL_GL04_LQBGSZGLLFET1508',
                'CG_LT_GL_GL04_LQBCSWDTE1509', 'CG_LT_GL_GL04_LQBGSZGLLFET1520', 'CG_LT_GL_GL04_LQBCSWDTE1510',
                'CG_LT_GL_GL04_LQBCSWDTE1523', 'CG_LT_GL_GL04_LQBGSZGLLFET1509', 'CG_LT_GL_GL04_LQBCSWDTE1512',
                'CG_LT_GL_GL04_LQBGSZGLLFET1513', 'CG_LT_GL_GL04_LQBGSZGLLFET1522', 'CG_LT_GL_GL04_LQBGSZGLLFET1514',
                'CG_LT_GL_GL04_21704WDPJ', 'CG_LT_GL_GL04_LQBGSZGLLFET1524', 'CG_LT_GL_GL04_LQBGSZGLLFET1511',
                'CG_LT_GL_GL04_LQBCSWDTE1520', 'CG_LT_GL_GL04_LQBGSZGLLFET1507', 'CG_LT_GL_GL04_LQBCSWDTE1503',
                'CG_LT_GL_GL04_LQBGSZGLLFET1510', 'CG_LT_GL_GL04_LQBCSWDTE1506', 'CG_LT_GL_GL04_LQBCSWDTE1508',
                'CG_LT_GL_GL04_LQBCSWDTE1518', 'CG_LT_GL_GL04_17187WDPJ', 'CG_LT_GL_GL04_19507WDPJ',
                'CG_LT_GL_GL04_24145WDPJ', 'CG_LT_GL_GL04_LQBCSWDTE1515', 'CG_LT_GL_GL04_LQBCSWDTE1516',
                'CG_LT_GL_GL04_LQBGSZGLLFET1517', 'CG_LT_GL_GL04_LQBCSWDTE1513', 'CG_LT_GL_GL04_LQBGSZGLLFET1518',
                'CG_LT_GL_GL04_LQBGSZGLLFET1512', 'CG_LT_GL_GL04_LQBCSWDTE1524', 'CG_LT_GL_GL04_LQBCSWDTE1521',
                'CG_LT_GL_GL04_LQBGSZGLLFET1506', 'CG_LT_GL_GL04_LQBGSZGLLFET1505', 'CG_LT_GL_GL04_LQBCSWDTE1514',
                'CG_LT_GL_GL04_LQSGSWD', 'CG_LT_GL_GL04_LQBGSZGLLFET1523', 'CG_LT_GL_GL04_LQBGSZGLLFET1521',
                'CG_LT_GL_GL04_LQBCSWDTE1511', 'CG_LT_GL_GL04_LQBCSWDTE1517', 'CG_LT_GL_GL04_LQBGSZGLLFET1519',
                'CG_LT_GL_GL04_LQBCSWDTE1519', 'CG_LT_GL_GL04_LQBCSWDTE1507', 'CG_LT_GL_GL04_LQBCSWDTE1505',
                'CG_LT_GL_GL04_LQBCSWDTE1522', 'CG_LT_GL_GL04_KTP']
        threads = []
        latch = CountDownLatch(len(inds))

        for ind in inds:
            dt_start_tz = date_util.dt_to_str(
                start - timedelta(hours=8),
                '%Y-%m-%dT%H:%M:%SZ')
            dt_end_tz = date_util.dt_to_str(
                end - timedelta(hours=8),
                '%Y-%m-%dT%H:%M:%SZ')
            params = dict(ind_name=ind, dt_start_tz=dt_start_tz, dt_end_tz=dt_end_tz,
                          dt_start=date_util.dt_to_str(start,'%Y-%m-%d %H:%M:%S'),
                          dt_end=date_util.dt_to_str(end,'%Y-%m-%d %H:%M:%S'),
                          influxdb=influxdb)
            if ind.startswith("CG_LT_GL_GL04_LQBGSZGLLFET") or ind.startswith("CG_LT_GL_GL04_LQBCSWDTE1524"):
                params["table"] = "CG"
            if ind == 'CG_LT_GL_GL04_KTP':
                params['db'] = "mysql"

            thread = SubThread(ind, latch, params)
            threads.append(thread)
            thread.start()

        latch.await()

        input = {thread.name: thread.result for thread in threads}
        for key, value in input.items():
            if value == 0: logging.info("{} is None".format(key))
        return input

    def cal_rrd_location(self, tongleng):
        """
        软熔带根部位置
        :return:
        """
        from interval3 import Interval, Inf
        contion_0 = Interval(-Inf, 0.44, upper_closed=False)
        contion_1 = Interval(0.44, 0.53, upper_closed=False)
        contion_2 = Interval(0.53, 0.58, upper_closed=False)
        contion_3 = Interval(0.58, 0.62, upper_closed=False)
        contion_4 = Interval(0.62, Inf, upper_closed=False)

        if tongleng in contion_0: return "炉腹17m上部"
        if tongleng in contion_1: return "炉腹17m上部"
        if tongleng in contion_2: return "炉腹19m上部"
        if tongleng in contion_3: return "炉身下部21m"
        if tongleng in contion_4: return "炉身下部24m"

    def cal_zha_pi(self, input):
        CG_LT_GL_GL04_24145WDPJ = input.get("CG_LT_GL_GL04_24145WDPJ")

        CG_LT_GL_GL04_21704WDPJ = input.get("CG_LT_GL_GL04_21704WDPJ")
        CG_LT_GL_GL04_19507WDPJ = input.get("CG_LT_GL_GL04_19507WDPJ")
        CG_LT_GL_GL04_17187WDPJ = input.get("CG_LT_GL_GL04_17187WDPJ")
        CG_LT_GL_GL04_LQBCSWDTE1503 = input.get("CG_LT_GL_GL04_LQBCSWDTE1503")
        CG_LT_GL_GL04_LQBCSWDTE1504 = input.get("CG_LT_GL_GL04_LQBCSWDTE1504")
        CG_LT_GL_GL04_LQBCSWDTE1505 = input.get("CG_LT_GL_GL04_LQBCSWDTE1505")
        CG_LT_GL_GL04_LQBCSWDTE1506 = input.get("CG_LT_GL_GL04_LQBCSWDTE1506")
        CG_LT_GL_GL04_LQBCSWDTE1507 = input.get("CG_LT_GL_GL04_LQBCSWDTE1507")
        CG_LT_GL_GL04_LQBCSWDTE1508 = input.get("CG_LT_GL_GL04_LQBCSWDTE1508")
        CG_LT_GL_GL04_LQBCSWDTE1509 = input.get("CG_LT_GL_GL04_LQBCSWDTE1509")
        CG_LT_GL_GL04_LQBCSWDTE1510 = input.get("CG_LT_GL_GL04_LQBCSWDTE1510")
        CG_LT_GL_GL04_LQBCSWDTE1511 = input.get("CG_LT_GL_GL04_LQBCSWDTE1511")
        CG_LT_GL_GL04_LQBCSWDTE1512 = input.get("CG_LT_GL_GL04_LQBCSWDTE1512")
        CG_LT_GL_GL04_LQBCSWDTE1513 = input.get("CG_LT_GL_GL04_LQBCSWDTE1513")
        CG_LT_GL_GL04_LQBCSWDTE1514 = input.get("CG_LT_GL_GL04_LQBCSWDTE1514")
        CG_LT_GL_GL04_LQBCSWDTE1515 = input.get("CG_LT_GL_GL04_LQBCSWDTE1515")
        CG_LT_GL_GL04_LQBCSWDTE1516 = input.get("CG_LT_GL_GL04_LQBCSWDTE1516")
        CG_LT_GL_GL04_LQBCSWDTE1517 = input.get("CG_LT_GL_GL04_LQBCSWDTE1517")
        CG_LT_GL_GL04_LQBCSWDTE1518 = input.get("CG_LT_GL_GL04_LQBCSWDTE1518")
        CG_LT_GL_GL04_LQBCSWDTE1519 = input.get("CG_LT_GL_GL04_LQBCSWDTE1519")
        CG_LT_GL_GL04_LQBCSWDTE1520 = input.get("CG_LT_GL_GL04_LQBCSWDTE1520")
        CG_LT_GL_GL04_LQBCSWDTE1521 = input.get("CG_LT_GL_GL04_LQBCSWDTE1521")
        CG_LT_GL_GL04_LQBCSWDTE1522 = input.get("CG_LT_GL_GL04_LQBCSWDTE1522")
        CG_LT_GL_GL04_LQBCSWDTE1523 = input.get("CG_LT_GL_GL04_LQBCSWDTE1523")
        CG_LT_GL_GL04_LQBCSWDTE1524 = input.get("CG_LT_GL_GL04_LQBCSWDTE1524")
        CG_LT_GL_GL04_LQSGSWD = input.get("CG_LT_GL_GL04_LQSGSWD")
        CG_LT_GL_GL04_LQBGSZGLLFET1505 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1505")
        CG_LT_GL_GL04_LQBGSZGLLFET1506 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1506")
        CG_LT_GL_GL04_LQBGSZGLLFET1507 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1507")
        CG_LT_GL_GL04_LQBGSZGLLFET1508 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1508")
        CG_LT_GL_GL04_LQBGSZGLLFET1509 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1509")
        CG_LT_GL_GL04_LQBGSZGLLFET1510 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1510")
        CG_LT_GL_GL04_LQBGSZGLLFET1511 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1511")
        CG_LT_GL_GL04_LQBGSZGLLFET1512 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1512")
        CG_LT_GL_GL04_LQBGSZGLLFET1513 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1513")
        CG_LT_GL_GL04_LQBGSZGLLFET1514 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1514")
        CG_LT_GL_GL04_LQBGSZGLLFET1515 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1515")
        CG_LT_GL_GL04_LQBGSZGLLFET1516 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1516")
        CG_LT_GL_GL04_LQBGSZGLLFET1517 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1517")
        CG_LT_GL_GL04_LQBGSZGLLFET1518 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1518")
        CG_LT_GL_GL04_LQBGSZGLLFET1519 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1519")
        CG_LT_GL_GL04_LQBGSZGLLFET1520 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1520")
        CG_LT_GL_GL04_LQBGSZGLLFET1521 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1521")
        CG_LT_GL_GL04_LQBGSZGLLFET1522 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1522")
        CG_LT_GL_GL04_LQBGSZGLLFET1523 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1523")
        CG_LT_GL_GL04_LQBGSZGLLFET1524 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1524")
        CG_LT_GL_GL04_LQBGSZGLLFET1525 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1525")

        return (((
                         CG_LT_GL_GL04_24145WDPJ + CG_LT_GL_GL04_21704WDPJ + CG_LT_GL_GL04_19507WDPJ + CG_LT_GL_GL04_17187WDPJ) / 4) - CG_LT_GL_GL04_LQSGSWD) / (
                       ((
                                CG_LT_GL_GL04_LQBCSWDTE1503 + CG_LT_GL_GL04_LQBCSWDTE1504 + CG_LT_GL_GL04_LQBCSWDTE1505 + CG_LT_GL_GL04_LQBCSWDTE1506 + CG_LT_GL_GL04_LQBCSWDTE1507 + CG_LT_GL_GL04_LQBCSWDTE1508 + CG_LT_GL_GL04_LQBCSWDTE1509 + CG_LT_GL_GL04_LQBCSWDTE1510 + CG_LT_GL_GL04_LQBCSWDTE1511 + CG_LT_GL_GL04_LQBCSWDTE1512 + CG_LT_GL_GL04_LQBCSWDTE1513 + CG_LT_GL_GL04_LQBCSWDTE1514 + CG_LT_GL_GL04_LQBCSWDTE1515 + CG_LT_GL_GL04_LQBCSWDTE1516 + CG_LT_GL_GL04_LQBCSWDTE1517 + CG_LT_GL_GL04_LQBCSWDTE1518 + CG_LT_GL_GL04_LQBCSWDTE1519 + CG_LT_GL_GL04_LQBCSWDTE1520 + CG_LT_GL_GL04_LQBCSWDTE1521 + CG_LT_GL_GL04_LQBCSWDTE1522 + CG_LT_GL_GL04_LQBCSWDTE1523 + CG_LT_GL_GL04_LQBCSWDTE1524) / 22 - CG_LT_GL_GL04_LQSGSWD) * (
                           (
                                   CG_LT_GL_GL04_LQBGSZGLLFET1505 + CG_LT_GL_GL04_LQBGSZGLLFET1506 + CG_LT_GL_GL04_LQBGSZGLLFET1507 + CG_LT_GL_GL04_LQBGSZGLLFET1508 + CG_LT_GL_GL04_LQBGSZGLLFET1509 + CG_LT_GL_GL04_LQBGSZGLLFET1510 + CG_LT_GL_GL04_LQBGSZGLLFET1511 + CG_LT_GL_GL04_LQBGSZGLLFET1512 + CG_LT_GL_GL04_LQBGSZGLLFET1513 + CG_LT_GL_GL04_LQBGSZGLLFET1514 + CG_LT_GL_GL04_LQBGSZGLLFET1515 + CG_LT_GL_GL04_LQBGSZGLLFET1516 + CG_LT_GL_GL04_LQBGSZGLLFET1517 + CG_LT_GL_GL04_LQBGSZGLLFET1518 + CG_LT_GL_GL04_LQBGSZGLLFET1519 + CG_LT_GL_GL04_LQBGSZGLLFET1520 + CG_LT_GL_GL04_LQBGSZGLLFET1521 + CG_LT_GL_GL04_LQBGSZGLLFET1522 + CG_LT_GL_GL04_LQBGSZGLLFET1523 + CG_LT_GL_GL04_LQBGSZGLLFET1524 + CG_LT_GL_GL04_LQBGSZGLLFET1525)) * 2200) * 0.5 * 100000000

    def cal_nianjie(self, input):
        CG_LT_GL_GL04_24145WDPJ = input.get("CG_LT_GL_GL04_24145WDPJ")
        CG_LT_GL_GL04_21704WDPJ = input.get("CG_LT_GL_GL04_21704WDPJ")
        CG_LT_GL_GL04_19507WDPJ = input.get("CG_LT_GL_GL04_19507WDPJ")
        CG_LT_GL_GL04_17187WDPJ = input.get("CG_LT_GL_GL04_17187WDPJ")
        CG_LT_GL_GL04_LQBCSWDTE1503 = input.get("CG_LT_GL_GL04_LQBCSWDTE1503")
        CG_LT_GL_GL04_LQBCSWDTE1504 = input.get("CG_LT_GL_GL04_LQBCSWDTE1504")
        CG_LT_GL_GL04_LQBCSWDTE1505 = input.get("CG_LT_GL_GL04_LQBCSWDTE1505")
        CG_LT_GL_GL04_LQBCSWDTE1506 = input.get("CG_LT_GL_GL04_LQBCSWDTE1506")
        CG_LT_GL_GL04_LQBCSWDTE1507 = input.get("CG_LT_GL_GL04_LQBCSWDTE1507")
        CG_LT_GL_GL04_LQBCSWDTE1508 = input.get("CG_LT_GL_GL04_LQBCSWDTE1508")
        CG_LT_GL_GL04_LQBCSWDTE1509 = input.get("CG_LT_GL_GL04_LQBCSWDTE1509")
        CG_LT_GL_GL04_LQBCSWDTE1510 = input.get("CG_LT_GL_GL04_LQBCSWDTE1510")
        CG_LT_GL_GL04_LQBCSWDTE1511 = input.get("CG_LT_GL_GL04_LQBCSWDTE1511")
        CG_LT_GL_GL04_LQBCSWDTE1512 = input.get("CG_LT_GL_GL04_LQBCSWDTE1512")
        CG_LT_GL_GL04_LQBCSWDTE1513 = input.get("CG_LT_GL_GL04_LQBCSWDTE1513")
        CG_LT_GL_GL04_LQBCSWDTE1514 = input.get("CG_LT_GL_GL04_LQBCSWDTE1514")
        CG_LT_GL_GL04_LQBCSWDTE1515 = input.get("CG_LT_GL_GL04_LQBCSWDTE1515")
        CG_LT_GL_GL04_LQBCSWDTE1516 = input.get("CG_LT_GL_GL04_LQBCSWDTE1516")
        CG_LT_GL_GL04_LQBCSWDTE1517 = input.get("CG_LT_GL_GL04_LQBCSWDTE1517")
        CG_LT_GL_GL04_LQBCSWDTE1518 = input.get("CG_LT_GL_GL04_LQBCSWDTE1518")
        CG_LT_GL_GL04_LQBCSWDTE1519 = input.get("CG_LT_GL_GL04_LQBCSWDTE1519")
        CG_LT_GL_GL04_LQBCSWDTE1520 = input.get("CG_LT_GL_GL04_LQBCSWDTE1520")
        CG_LT_GL_GL04_LQBCSWDTE1521 = input.get("CG_LT_GL_GL04_LQBCSWDTE1521")
        CG_LT_GL_GL04_LQBCSWDTE1522 = input.get("CG_LT_GL_GL04_LQBCSWDTE1522")
        CG_LT_GL_GL04_LQBCSWDTE1523 = input.get("CG_LT_GL_GL04_LQBCSWDTE1523")
        CG_LT_GL_GL04_LQBCSWDTE1524 = input.get("CG_LT_GL_GL04_LQBCSWDTE1524")
        CG_LT_GL_GL04_LQSGSWD = input.get("CG_LT_GL_GL04_LQSGSWD")
        CG_LT_GL_GL04_LQBGSZGLLFET1505 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1505")
        CG_LT_GL_GL04_LQBGSZGLLFET1506 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1506")
        CG_LT_GL_GL04_LQBGSZGLLFET1507 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1507")
        CG_LT_GL_GL04_LQBGSZGLLFET1508 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1508")
        CG_LT_GL_GL04_LQBGSZGLLFET1509 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1509")
        CG_LT_GL_GL04_LQBGSZGLLFET1510 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1510")
        CG_LT_GL_GL04_LQBGSZGLLFET1511 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1511")
        CG_LT_GL_GL04_LQBGSZGLLFET1512 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1512")
        CG_LT_GL_GL04_LQBGSZGLLFET1513 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1513")
        CG_LT_GL_GL04_LQBGSZGLLFET1514 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1514")
        CG_LT_GL_GL04_LQBGSZGLLFET1515 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1515")
        CG_LT_GL_GL04_LQBGSZGLLFET1516 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1516")
        CG_LT_GL_GL04_LQBGSZGLLFET1517 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1517")
        CG_LT_GL_GL04_LQBGSZGLLFET1518 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1518")
        CG_LT_GL_GL04_LQBGSZGLLFET1519 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1519")
        CG_LT_GL_GL04_LQBGSZGLLFET1520 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1520")
        CG_LT_GL_GL04_LQBGSZGLLFET1521 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1521")
        CG_LT_GL_GL04_LQBGSZGLLFET1522 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1522")
        CG_LT_GL_GL04_LQBGSZGLLFET1523 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1523")
        CG_LT_GL_GL04_LQBGSZGLLFET1524 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1524")
        CG_LT_GL_GL04_LQBGSZGLLFET1525 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1525")

        return ((sum([CG_LT_GL_GL04_24145WDPJ, CG_LT_GL_GL04_21704WDPJ, CG_LT_GL_GL04_19507WDPJ,
                      CG_LT_GL_GL04_17187WDPJ]) / 4) - CG_LT_GL_GL04_LQSGSWD) / (((sum([CG_LT_GL_GL04_LQBCSWDTE1503,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1504,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1505,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1506,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1507,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1508,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1509,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1510,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1511,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1512,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1513,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1514,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1515,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1516,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1517,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1518,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1519,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1520,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1521,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1522,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1523,
                                                                                        CG_LT_GL_GL04_LQBCSWDTE1524]) / 22) - CG_LT_GL_GL04_LQSGSWD) * sum(
            [CG_LT_GL_GL04_LQBGSZGLLFET1505, CG_LT_GL_GL04_LQBGSZGLLFET1506, CG_LT_GL_GL04_LQBGSZGLLFET1507,
             CG_LT_GL_GL04_LQBGSZGLLFET1508, CG_LT_GL_GL04_LQBGSZGLLFET1509, CG_LT_GL_GL04_LQBGSZGLLFET1510,
             CG_LT_GL_GL04_LQBGSZGLLFET1511, CG_LT_GL_GL04_LQBGSZGLLFET1512, CG_LT_GL_GL04_LQBGSZGLLFET1513,
             CG_LT_GL_GL04_LQBGSZGLLFET1514, CG_LT_GL_GL04_LQBGSZGLLFET1515, CG_LT_GL_GL04_LQBGSZGLLFET1516,
             CG_LT_GL_GL04_LQBGSZGLLFET1517, CG_LT_GL_GL04_LQBGSZGLLFET1518, CG_LT_GL_GL04_LQBGSZGLLFET1519,
             CG_LT_GL_GL04_LQBGSZGLLFET1520, CG_LT_GL_GL04_LQBGSZGLLFET1521, CG_LT_GL_GL04_LQBGSZGLLFET1522,
             CG_LT_GL_GL04_LQBGSZGLLFET1523, CG_LT_GL_GL04_LQBGSZGLLFET1524,
             CG_LT_GL_GL04_LQBGSZGLLFET1525]) / 1.7 / 2 * 4.1718 / 44)

    def cal_tongleng(self, input):
        CG_LT_GL_GL04_LQBCSWDTE1503 = input.get("CG_LT_GL_GL04_LQBCSWDTE1503")
        CG_LT_GL_GL04_LQBCSWDTE1504 = input.get("CG_LT_GL_GL04_LQBCSWDTE1504")
        CG_LT_GL_GL04_LQBCSWDTE1505 = input.get("CG_LT_GL_GL04_LQBCSWDTE1505")
        CG_LT_GL_GL04_LQBCSWDTE1506 = input.get("CG_LT_GL_GL04_LQBCSWDTE1506")
        CG_LT_GL_GL04_LQBCSWDTE1507 = input.get("CG_LT_GL_GL04_LQBCSWDTE1507")
        CG_LT_GL_GL04_LQBCSWDTE1508 = input.get("CG_LT_GL_GL04_LQBCSWDTE1508")
        CG_LT_GL_GL04_LQBCSWDTE1509 = input.get("CG_LT_GL_GL04_LQBCSWDTE1509")
        CG_LT_GL_GL04_LQBCSWDTE1510 = input.get("CG_LT_GL_GL04_LQBCSWDTE1510")
        CG_LT_GL_GL04_LQBCSWDTE1511 = input.get("CG_LT_GL_GL04_LQBCSWDTE1511")
        CG_LT_GL_GL04_LQBCSWDTE1512 = input.get("CG_LT_GL_GL04_LQBCSWDTE1512")
        CG_LT_GL_GL04_LQBCSWDTE1513 = input.get("CG_LT_GL_GL04_LQBCSWDTE1513")
        CG_LT_GL_GL04_LQBCSWDTE1514 = input.get("CG_LT_GL_GL04_LQBCSWDTE1514")
        CG_LT_GL_GL04_LQBCSWDTE1515 = input.get("CG_LT_GL_GL04_LQBCSWDTE1515")
        CG_LT_GL_GL04_LQBCSWDTE1516 = input.get("CG_LT_GL_GL04_LQBCSWDTE1516")
        CG_LT_GL_GL04_LQBCSWDTE1517 = input.get("CG_LT_GL_GL04_LQBCSWDTE1517")
        CG_LT_GL_GL04_LQBCSWDTE1518 = input.get("CG_LT_GL_GL04_LQBCSWDTE1518")
        CG_LT_GL_GL04_LQBCSWDTE1519 = input.get("CG_LT_GL_GL04_LQBCSWDTE1519")
        CG_LT_GL_GL04_LQBCSWDTE1520 = input.get("CG_LT_GL_GL04_LQBCSWDTE1520")
        CG_LT_GL_GL04_LQBCSWDTE1521 = input.get("CG_LT_GL_GL04_LQBCSWDTE1521")
        CG_LT_GL_GL04_LQBCSWDTE1522 = input.get("CG_LT_GL_GL04_LQBCSWDTE1522")
        CG_LT_GL_GL04_LQBCSWDTE1523 = input.get("CG_LT_GL_GL04_LQBCSWDTE1523")
        CG_LT_GL_GL04_LQBCSWDTE1524 = input.get("CG_LT_GL_GL04_LQBCSWDTE1524")
        CG_LT_GL_GL04_LQSGSWD = input.get("CG_LT_GL_GL04_LQSGSWD")
        CG_LT_GL_GL04_LQBGSZGLLFET1505 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1505")
        CG_LT_GL_GL04_LQBGSZGLLFET1506 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1506")
        CG_LT_GL_GL04_LQBGSZGLLFET1507 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1507")
        CG_LT_GL_GL04_LQBGSZGLLFET1508 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1508")
        CG_LT_GL_GL04_LQBGSZGLLFET1509 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1509")
        CG_LT_GL_GL04_LQBGSZGLLFET1510 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1510")
        CG_LT_GL_GL04_LQBGSZGLLFET1511 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1511")
        CG_LT_GL_GL04_LQBGSZGLLFET1512 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1512")
        CG_LT_GL_GL04_LQBGSZGLLFET1513 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1513")
        CG_LT_GL_GL04_LQBGSZGLLFET1514 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1514")
        CG_LT_GL_GL04_LQBGSZGLLFET1515 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1515")
        CG_LT_GL_GL04_LQBGSZGLLFET1516 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1516")
        CG_LT_GL_GL04_LQBGSZGLLFET1517 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1517")
        CG_LT_GL_GL04_LQBGSZGLLFET1518 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1518")
        CG_LT_GL_GL04_LQBGSZGLLFET1519 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1519")
        CG_LT_GL_GL04_LQBGSZGLLFET1520 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1520")
        CG_LT_GL_GL04_LQBGSZGLLFET1521 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1521")
        CG_LT_GL_GL04_LQBGSZGLLFET1522 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1522")
        CG_LT_GL_GL04_LQBGSZGLLFET1523 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1523")
        CG_LT_GL_GL04_LQBGSZGLLFET1524 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1524")
        CG_LT_GL_GL04_LQBGSZGLLFET1525 = input.get("CG_LT_GL_GL04_LQBGSZGLLFET1525")
        CG_LT_GL_GL04_RFH = input.get("CG_LT_GL_GL04_RFH")

        return ((((CG_LT_GL_GL04_LQBCSWDTE1503 +
                   CG_LT_GL_GL04_LQBCSWDTE1504 +
                   CG_LT_GL_GL04_LQBCSWDTE1505 +
                   CG_LT_GL_GL04_LQBCSWDTE1506 +
                   CG_LT_GL_GL04_LQBCSWDTE1507 +
                   CG_LT_GL_GL04_LQBCSWDTE1508 +
                   CG_LT_GL_GL04_LQBCSWDTE1509 +
                   CG_LT_GL_GL04_LQBCSWDTE1510 +
                   CG_LT_GL_GL04_LQBCSWDTE1511 +
                   CG_LT_GL_GL04_LQBCSWDTE1512 +
                   CG_LT_GL_GL04_LQBCSWDTE1513 +
                   CG_LT_GL_GL04_LQBCSWDTE1514 +
                   CG_LT_GL_GL04_LQBCSWDTE1515 +
                   CG_LT_GL_GL04_LQBCSWDTE1516 +
                   CG_LT_GL_GL04_LQBCSWDTE1517 +
                   CG_LT_GL_GL04_LQBCSWDTE1518 +
                   CG_LT_GL_GL04_LQBCSWDTE1519 +
                   CG_LT_GL_GL04_LQBCSWDTE1520 +
                   CG_LT_GL_GL04_LQBCSWDTE1521 +
                   CG_LT_GL_GL04_LQBCSWDTE1522 +
                   CG_LT_GL_GL04_LQBCSWDTE1523 +
                   CG_LT_GL_GL04_LQBCSWDTE1524) / 22) - CG_LT_GL_GL04_LQSGSWD) * (
                        CG_LT_GL_GL04_LQBGSZGLLFET1505 + CG_LT_GL_GL04_LQBGSZGLLFET1506 + CG_LT_GL_GL04_LQBGSZGLLFET1507 + CG_LT_GL_GL04_LQBGSZGLLFET1508 + CG_LT_GL_GL04_LQBGSZGLLFET1509 + CG_LT_GL_GL04_LQBGSZGLLFET1510 + CG_LT_GL_GL04_LQBGSZGLLFET1511 + CG_LT_GL_GL04_LQBGSZGLLFET1512 + CG_LT_GL_GL04_LQBGSZGLLFET1513 + CG_LT_GL_GL04_LQBGSZGLLFET1514 + CG_LT_GL_GL04_LQBGSZGLLFET1515 + CG_LT_GL_GL04_LQBGSZGLLFET1516 + CG_LT_GL_GL04_LQBGSZGLLFET1517 + CG_LT_GL_GL04_LQBGSZGLLFET1518 + CG_LT_GL_GL04_LQBGSZGLLFET1519 + CG_LT_GL_GL04_LQBGSZGLLFET1520 + CG_LT_GL_GL04_LQBGSZGLLFET1521 + CG_LT_GL_GL04_LQBGSZGLLFET1522 + CG_LT_GL_GL04_LQBGSZGLLFET1523 + CG_LT_GL_GL04_LQBGSZGLLFET1524 + CG_LT_GL_GL04_LQBGSZGLLFET1525)) / (
                   CG_LT_GL_GL04_RFH)

if __name__ == '__main__':
    z = ZhaPiComponent()
    z.generate2(None,None)
